/** ------------------------------------------------------------------------
 *	VisualGroup JavaScript prototype Classes, version draf
 *  (c) 2006 Bill Vison <vison_ruan@126.com>
 *
 *
/*--------------------------------------------------------------------------*/

/**
 * @Prototype Class : "Validator.ValidatorMethods"
 * @prerequire class : 	"Vsg.Common.Logger"
 						
 */
using("Vsg.Common.Logger");
 
Vsg.Validator.ValidatorMethods = Class.create();
Vsg.Validator.ValidatorMethods.prototype.initialize = function() {
	
	// set the prompt status
	this.prom = true;
	
	this.setPrompt = function(prom) {
		this.prom = prom;
	}
	
	this.getPrompt = function() {
		return this.prom;
	}
	
	this.selected = function($checkBoxNames) {
		var result = false;
		var $checkBoxes = document.getElementsByName($checkBoxNames);
		for (var $i = 0 ; $i < $checkBoxes.length ;$i++) {
			if ($checkBoxes[$i].checked) {
				result = true;
				break;
			}
		}

		if (!result) {
			alert("没有选择一条数据项！请至少选择一条！");
		}
		// debug show
		Vsg.Common.Logger.trace("[Vsg.Validator.ValidatorMethods.selectNone()] : " + result);
		
		return result;
	}
	
	this.selectOnly = function($checkBoxNames) {
		var $result = true;
		var $count = 0;
		var $checkBoxes = document.getElementsByName($checkBoxNames);
		for (var $i = 0 ; $i < $checkBoxes.length ;$i++) {
			if ($checkBoxes[$i].checked) {
				$count++;
			}
			if ($count > 1) {
				$result = false;
				break;
			}
		}
		
		if (!$result) {
			alert("你的选择多于一项，请确定只选一项!");
		}
		// debug show
		Vsg.Common.Logger.trace("[Vsg.Validator.ValidatorMethods.selectOnly()] : " + $result);
		
		return $result;	
	}
	
	this.checkNotEmpty = function($elem , $message) {
		var $result = false;
		// get the input type
		if ($elem.tagName == "INPUT" || $elem.tagName == "TEXTAREA") {
			if ($elem.value != "") {
				$result = true;
			}
		}

		
		if (!$result ) {
			var msg  = "输入项 ['"+$elem.name+"'] 不能为空!";
			if ($message != null) {
				msg = $message;
			}
			alert(msg);
			
			if ($elem.tagName == "INPUT" || $elem.tagName == "TEXTAREA") {
				$elem.focus();
			}
		}
		
		return $result;
	}
	
	this.checkIsNumber = function($elem) {
		var $result = false;
		
		// defined local value
		var $value;
		if ($elem.tagName == "INPUT") {
			$value = $elem.value;
		}
		
		var re = new RegExp("^\\d+$","ig");
		$result = re.test($value);
		if (!$result) {
			var msg  = "输入项 ['"+$elem.name+"'] 不是全数字，请重新输入!";
			alert(msg);
			
			if ($elem.tagName == "INPUT") {
				$elem.focus();
			}
		}		
		
		return $result;
	}
	
	this.checkIsEmail = function($elem) {
	}
	
	this.checkValueNotEmpty = function($value , $custMessage) {
		var $result = false;
		if ($value == null || $value == "") {
			var $msg = "请指定值!";		// defined default message
			if ($custMessage != null) {
				$msg = $custMessage;
			}
			alert($msg);
		} else {
			$result = true;
		}
		return $result
	}
	

}